/**
 * 
 */
package com.thor.palmmall.rest.brand;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.entity.UCN;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.palmmall.rest.brand.converter.ABrandConverter;
import com.thor.palmmall.rest.brand.converter.SBrandConverter;
import com.thor.palmmall.rest.brand.converter.SBrandQueryParamConverter;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.brand.SBrand;
import com.thor.palmmall.spi.brand.SBrandService;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;

/**
 * @author Captain
 *
 * @since 1.0
 */
public class ABrandServiceImpl implements ABrandService {

  @Autowired
  private SBrandService brandService;
  @Autowired
  private SBasicService sBasicService;

  @Override
  public QueryResult<ABrand> query(ABrandQueryParam queryParam) throws ThorServiceException {
    SResponse<SQueryResult<SBrand>> resp = brandService
        .query(SBrandQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return SQueryResultConverter.convert(resp.getBody(), ABrandConverter.getInstance());
    } else {
      throw new ThorServiceException("无法查询品牌：" + resp.getMessage());
    }
  }

  @Override
  public ABrand get(String id, AUserGroupLoadParam queryParam) throws ThorServiceException {
    SResponse<SBrand> resp = brandService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return ABrandConverter.getInstance().convert(resp.getBody());
    } else {
      throw new ThorServiceException("无法找到指定ID的品牌信息：" + resp.getMessage());
    }
  }

  @Override
  public String save(ABrand Brand, BeanOperateInfo operateInfo) throws ThorServiceException {
    SResponse<String> resp = brandService.save(SBrandConverter.getInstance().convert(Brand),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("无法保存品牌:" + resp.getMessage());
    }
  }

  @Override
  public List<String> grades(String category) throws ThorServiceException {
    SResponse<List<String>> sResponse = sBasicService.typeTag(category, null);
    if (sResponse.isSuccess()) {
      return sResponse.getBody();
    } else {
      throw new ThorServiceException(sResponse.getMessage());
    }
  }

}
